package com.hmy.uaa.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hmy.uaa.bo.request.DepartmentRequest;
import com.hmy.uaa.common.security.UserOAuth2Details;
import com.hmy.uaa.mapper.DepartmentMapper;
import com.hmy.uaa.po.Department;
import com.hmy.common.exception.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 部门信息
 */
@Service
public class DepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private UserService userService;

    /**
     * 新增部门
     *
     * @param departmentRequest
     */
    public void save(DepartmentRequest departmentRequest) {
        if (illegalName(departmentRequest)) {
            throw new ClientException("部门名称已存在");
        }
        Department department = convertBean(departmentRequest);
        department.setCreateTime(LocalDateTime.now());
        department.setCompanyId( UserOAuth2Details.getUserDTO().getCompanyId());
        departmentMapper.insert(department);
    }

    /**
     * 修改部门
     *
     * @param departmentRequest
     */
    public void update(DepartmentRequest departmentRequest) {

        if (illegalName(departmentRequest)) {
            throw new ClientException("部门名称已存在");
        }

        Department department = convertBean(departmentRequest);
        department.setId(departmentRequest.getId());
        department.setUpdateTime(LocalDateTime.now());
        departmentMapper.updateById(department);
    }

    /**
     * 删除部门
     *
     * @param id 部门id
     */
    public void delete(Long id) {

        //判断该部门是否存在二级部门
        Long companyId = UserOAuth2Details.getUserDTO().getCompanyId();
        Integer countDepartment = departmentMapper.count(id,companyId);
        if (countDepartment > 1) {
            throw new ClientException("删除失败，该部门有二级部门");
        }

        //判断该部门是否存在用户
        boolean isExistsUser = userService.isExistsUser(id);
        if (isExistsUser) {
            throw new ClientException("删除失败，该部门有所属用户");
        }

        //删除部门
        LambdaQueryWrapper<Department> queryDepartmentWrapper = Wrappers.<Department>lambdaQuery()
                .eq(Department::getId, id);
        departmentMapper.delete(queryDepartmentWrapper);
    }

    /**
     * 查询部门
     *
     * @param id 部门id
     * @return List<Department>
     */
    public List<Department> queryList(Long id) {
        Long companyId = UserOAuth2Details.getUserDTO().getCompanyId();
        return departmentMapper.queryList(id,companyId);
    }

    /**
     * 获取部门信息
     *
     * @param id 部门id
     */
    public Department get(Long id) {

        LambdaQueryWrapper<Department> queryWrapper = Wrappers.<Department>lambdaQuery()
                .eq(Department::getId, id);
        return departmentMapper.selectOne(queryWrapper);
    }

    /**
     * 是否非法部门名称
     */
    private boolean illegalName(DepartmentRequest departmentRequest) {
        LambdaQueryWrapper<Department> queryWrapper = Wrappers.<Department>lambdaQuery()
                .eq(Department::getName, departmentRequest.getName());

        if (Objects.nonNull(departmentRequest.getParentId())) {
            queryWrapper.eq(Department::getParentId, departmentRequest.getParentId());
        }
        return departmentMapper.selectCount(queryWrapper) > 0;
    }

    /**
     * 转换bean
     *
     * @param departmentRequest
     * @return departmentPO
     */
    private Department convertBean(DepartmentRequest departmentRequest) {
        Department department = new Department();
        department.setName(departmentRequest.getName());
        department.setParentId(departmentRequest.getParentId());

        return department;
    }


}
